package com.bafa.front.presentationbeans;

import java.math.BigDecimal;

import com.bafa.objects.team.Team;
import com.bafa.objects.team.TeamSeasonStats;
import com.bafa.objects.team.TeamStats;
import com.bafa.services.RetrieveServices;

public class LeaguePositionBean implements Comparable {

	private String name;
	private int id;
	private int points;
	private int goalsFor;
	private int goalsAgainst;
	private int goalDifference;
	private int won;
	private int drawn;
	private int lost;
	private int position;
	private float percentageWins;
	private float averageGoalsFor;
	private float averageGoalsAgainst;
	
	public float getAverageGoalsFor() {
		BigDecimal amount = new BigDecimal(0);
		if(goalsFor>0){
			amount = new BigDecimal(goalsFor).setScale(2).divide(new BigDecimal(getGamesPlayed()).setScale(2),2,BigDecimal.ROUND_UP);
		}
		return amount.floatValue();
	}

	public void setAverageGoalsFor(float averageGoals) {
		this.averageGoalsFor = averageGoals;
	}

	public float getPercentageWins() {
		BigDecimal amount = new BigDecimal(0.0);
        if(getGamesPlayed()>0)
            amount = new BigDecimal(won).setScale(2).divide(new BigDecimal(getGamesPlayed()).setScale(2),2,BigDecimal.ROUND_UP).multiply(new BigDecimal(100));
return amount.floatValue();
	}

	public void setPercentageWins(float percentageWins) {
		this.percentageWins = percentageWins;
	}

	public LeaguePositionBean(int teamId, int seasonId){
		Team team = RetrieveServices.getTeam(""+teamId);
		TeamSeasonStats seasonStats = (TeamSeasonStats)team.getStats().getSeasonStats().get(seasonId);
		goalsAgainst = seasonStats.getLeagueGoalsConceded();
		goalsFor = seasonStats.getLeagueGoalsScored();
		won = seasonStats.getLeagueGamesWon();
		lost = seasonStats.getLeagueGamesLost();
		drawn = seasonStats.getLeagueGamesDrawn();
		id = team.getId();
		name = team.getName();
		points = (won*3)+drawn;
		goalDifference = goalsFor-goalsAgainst;
	}
	
	public LeaguePositionBean(int teamId){
		Team team = RetrieveServices.getTeam(""+teamId);
		TeamStats seasonStats = (TeamStats)team.getStats();
		goalsAgainst = seasonStats.getGoalsConceded();
		goalsFor = seasonStats.getGoalsScored();
		won = seasonStats.getGamesWon();
		lost = seasonStats.getGamesLost();
		drawn = seasonStats.getGamesDrawn();
		id = team.getId();
		name = team.getName();
		points = (won*3)+drawn;
		goalDifference = goalsFor-goalsAgainst;
		
	}
	
	public int compareTo(Object obj) {
		LeaguePositionBean bean = (LeaguePositionBean)obj;
		if(this.points!=bean.getPoints())
			return bean.getPoints()-this.points;
		else if(this.goalDifference!= bean.getGoalDifference())
			return bean.getGoalDifference()-this.goalDifference;
		else if(this.goalsFor!=bean.getGoalsFor())
			return bean.getGoalsFor()-this.goalsFor;
		else			
			return 1;
	}

	public int getGoalDifference() {
		return goalDifference;
	}

	public void setGoalDifference(int goalDifference) {
		this.goalDifference = goalDifference;
	}

	public int getGoalsAgainst() {
		return goalsAgainst;
	}

	public void setGoalsAgainst(int goalsAgainst) {
		this.goalsAgainst = goalsAgainst;
	}

	public int getGoalsFor() {
		return goalsFor;
	}

	public void setGoalsFor(int goalsFor) {
		this.goalsFor = goalsFor;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPoints() {
		return points;
	}

	public void setPoints(int points) {
		this.points = points;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public int getDrawn() {
		return drawn;
	}

	public void setDrawn(int drawn) {
		this.drawn = drawn;
	}

	public int getLost() {
		return lost;
	}

	public void setLost(int lost) {
		this.lost = lost;
	}

	public int getWon() {
		return won;
	}

	public void setWon(int won) {
		this.won = won;
	}
	
	public int getGamesPlayed(){
		return won+lost+drawn;
	}

	public float getAverageGoalsAgainst() {
		BigDecimal amount = new BigDecimal(0);
		if(goalsAgainst>0)
			amount = new BigDecimal(goalsAgainst).setScale(2).divide(new BigDecimal(getGamesPlayed()).setScale(2),2,BigDecimal.ROUND_UP);
		return amount.floatValue();
	}

	public void setAverageGoalsAgainst(float averageGoalsAgainst) {
		this.averageGoalsAgainst = averageGoalsAgainst;
	}

}
